<template>
  <el-dialog title="设置角色" :visible.sync="dialogShow" width="50%" @close="closeDialog">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="checkList">
      <!-- 选项 -->
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{
          item.name
        }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="setRoleOk">确定</el-button>
        <el-button size="small" @click="dialogShow = false">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleListApi } from '@/api/setting'
import { assignRoles, getEmployeesInfoApi } from '@/api/employees'
export default {
  name: 'SetRole',
  data() {
    return {
      dialogShow: false,
      roleList: [],
      checkList: [],
      userId: null // 当前员工的Id
    }
  },
  // created() {
  //   this.getAllRoleList()
  // },
  methods: {
    // 设置员工角色
    async setRoleOk() {
      await assignRoles({
        id: this.userId,
        roleIds: this.checkList
      })
      this.$message.success('设置成功')
      this.dialogShow = false
    },
    closeDialog() {
      this.checkList = []
      this.userId = null
    },
    async getAllRoleList() {
      const { rows } = await getRoleListApi({
        page: 1,
        size: 100
      })
      this.roleList = rows
    },
    async getCurrentRole(id) {
      const res = await getEmployeesInfoApi(id)
      this.userId = id
      console.log(res)
      this.checkList = res.roleIds || []
    }
  }
}
</script>

<style>

</style>
